package com.vision.world.survey.adapters;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.vision.world.survey.R;
import com.vision.world.survey.fragments.BoundaryFragment;
import com.vision.world.survey.managers.QueryManager;
import com.vision.world.survey.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * Created by Administrator on 2017/10/18.
 */

public class ResultAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Map<String,Object>> items=new ArrayList<>();

    public ResultAdapter(LayoutInflater inflater, List<Map<String,Object>> item){
        items=item;
        this.mInflater =inflater;
    }

    public void setItem(List<Map<String,Object>> list){
        items= list;  //QueryManager.getInstance().SearchResultFromOperationLayer;
    }

    @Override
    public int getCount() {
        // 如果已经到了最后一屏，且应用程序数量不能整除NUMBER_PER_SCREEN
        if (CommonUtil.screenNo == CommonUtil.screenCount - 1
                && items.size() %CommonUtil. NUMBER_PER_SCREEN != 0) {
            // 最后一屏显示的程序数为应用程序的数量对NUMBER_PER_SCREEN求余
            return items.size() % CommonUtil.NUMBER_PER_SCREEN;
        }
        // 否则每屏显示的程序数量为NUMBER_PER_SCREEN
        return CommonUtil.NUMBER_PER_SCREEN;
    }

    @Override
    public Map<String,Object> getItem(int position)
    {
        int in=CommonUtil.screenNo * CommonUtil.NUMBER_PER_SCREEN + position;
        Log.i("itemss","item index="+in);
        Map<String,Object> re= QueryManager.getInstance().SearchResultFromOperationLayer.get(in);
        return re;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Log.i("boundary","index="+position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.tiem_gv_viewswitch, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if(items.size()>0) {
            int in=CommonUtil.screenNo * CommonUtil.NUMBER_PER_SCREEN + position+1;
            holder.txtIndex.setText(in+" / "+QueryManager.getInstance().SearchResultFromOperationLayer.size());
            Map<String, Object> result = getItem(position);
            Set<String> resultk = result.keySet();
            int i = 0;
            for (String key : resultk) {
                if (!key.equals("FID") && !key.equals("GOE") && !key.equals("LAYERNAME") && !key.equals("OBJECTID") && !key.equals("Shape")) {
                    if(key.equals("Distance")) {
                        holder.txtList.get(i).setText("距红线区距离：");
                        i++;
                        Object a = result.get(key);
                        if (a == null) {
                            holder.txtList.get(i).setText(" ");
                        } else {
                            if(Double.parseDouble(a.toString())<0){
                                holder.txtList.get(i).setText("在红线区內");
                            }else {
                                String str=a.toString();
                                str=str.substring(0,str.indexOf("."))+"米";
                                holder.txtList.get(i).setText(str);
                            }
                        }
                        i++;
                    }
                    else if(key.equals("类型") || key.equals("类别")){
                        holder.txtList.get(i).setText(key + "：");
                        i++;
                        Object a = result.get(key);
                        if (a == null) {
                            holder.txtList.get(i).setText(" ");
                        } else {
                            holder.txtList.get(i).setText(a.toString());
                        }
                        i++;
                    }
                }
            }

        }
        return convertView;
    }

    static class ViewHolder {
        final TextView txtKey1;
        final TextView txtKey2;
        final TextView txtKey3;
        final TextView txtValue1;
        final TextView txtValue2;
        final TextView txtValue3;
        final TextView txtIndex;
        final List<TextView> txtList=new ArrayList<>();
        ViewHolder(View view) {
            txtKey1=(TextView)view.findViewById(R.id.txt_result_key1);
            txtKey2=(TextView)view.findViewById(R.id.txt_result_key2);
            txtKey3=(TextView)view.findViewById(R.id.txt_result_key3);
            txtValue1=(TextView)view.findViewById(R.id.txt_result_value1);
            txtValue2=(TextView)view.findViewById(R.id.txt_result_value2);
            txtValue3=(TextView)view.findViewById(R.id.txt_result_value3);
            txtIndex=(TextView)view.findViewById(R.id.txt_index_count);
            txtList.add(txtKey1);
            txtList.add(txtValue1);
            txtList.add(txtKey2);
            txtList.add(txtValue2);
            txtList.add(txtKey3);
            txtList.add(txtValue3);
        }
    }
}
